• Jump To … +
    main.js separate.js single.js web-apg-api.js main.js web-conv-api.js ast.js csv.js dangling-else.js display.js flags.js float.js limits.js main.js multiline-mode.js recursive.js replace.js rules.js split.js testonly.js trace.js udt.js unicode.js web-email.js word-boundaries.js main.js phone-number.js web-main.js web-phone-number.js main.js phone-number.js setup.js translate.js xml.js branch-fail-grammar.js main.js parent-mode-grammar.js setup.js universal-mode-grammar.js colors-app.js colors-callbacks.js colors.js main.js more-app.js more-setup.js more.js ast-callbacks.js bad-input.js basic.js ini-file.js main.js parser-callbacks.js setup.js trace.js anbncn.js and.js c-comment.js compound.js main.js nested.js not.js setup.js boundaries-grammar.js boundaries.js comment-grammar.js comment.js main.js negative-grammar.js negative.js positive-grammar.js positive.js setup.js main.js odata-grammar.js run.js setup.js area-code.js lookaround.js main.js phone-number.js setup.js simple.js all-operators.js default.js fancy-number.js limited-lines.js main.js select-operators.js select-rules.js setup.js main.js minimal.js parent-u.js parent.js phone-number.js setup.js stats.js trace.js universal-u.js universal.js callbacks.js grammar.js main.js parser.js writeHtml.js LICENSE.md README.md index.md
  • split.js

  • §
    /*  *************************************************************************************
     *   copyright: Copyright (c) 2021 Lowell D. Thomas, all rights reserved
     *     license: BSD-2-Clause (https://opensource.org/licenses/BSD-2-Clause)
     *   ********************************************************************************* */
  • §

    This module demonstrates the split() function. It is roughly equivalent to the JavaScript string String.split(regex[, limit]) function (It loosely follows the MDN description.) Let:

    exp = new apgExp(grammar,flags);
    rstr = exp.split(str[, limit]);
    

    If str is omitted, null or an empty string, an array with one element is returned, [""]. Otherwise, exp.exec(str) is called in global mode. If a one or more matched phrases are found, they are removed from the string and the substrings are returned in an array. If no matched phrases are found, the array contains one element consisting of the entire string, ["str"]. Empty string matches will split the string and advance lastIndex by one character. That means, for example, the grammar rule=""\n would match the empty string at every character and an array of all characters would be returned. It would be similar to calling the JavaScript function str.split(""). Unlike the JavaScript function, capturing parentheses (rules) are not spliced into the output string. split() ignores all flags except u. It will throw an exception if the unicode flag is set. If the limit argument is used, it must be a positive number and no more than limit matches will be

    (function split() {
      try {
        const { apgExp: ApgExp } = require('apg-js');
    
        let grammar = '';
        let exp;
        let flags;
        let str;
        let restr;
        grammar += 'rule = ""\n';
        exp = new ApgExp(grammar, flags);
        console.log();
        str = 'abcxyz';
  • §

    This module demonstrates the split() function. It is roughly equivalent to the JavaScript string String.split(regex[, limit]) function

        console.log();
        console.log('Demonstrate the `split()` function.');
        console.log('It is roughly equivalent to the JavaScript string `String.split(regex[, limit])` function.');
        console.log();
        console.log(` grammar: ${exp.source}`);
        restr = exp.split(str);
        console.log('        : split into characters');
        console.log(`   input: ${str}`);
        console.log(`   split: [${restr}]`);
    
        restr = exp.split(str, 3);
        console.log();
        console.log('        : limit to 3 matched phrases');
        console.log(`   input: ${str}`);
        console.log(`   split: [${restr}]`);
    
        str = undefined;
        restr = exp.split(str);
        console.log();
        console.log('        : no input');
        console.log(`   input: ${str}`);
        console.log(`   split: [${restr}]`);
        console.log(`  length: [${restr.length}]`);
    
        str = 'abcxyz';
        grammar = 'rule = "d"\n';
        exp = new ApgExp(grammar, flags);
        restr = exp.split(str);
        console.log();
        console.log('        : no matches');
        console.log(`   input: ${str}`);
        console.log(`   split: [${restr}]`);
      } catch (e) {
        console.log(`EXCEPTION: ${e.message}`);
      }
    })();